
import 'dart:convert';

import 'package:bot_toast/bot_toast.dart';
import 'package:exams_app/gen_a/A.dart';
import 'package:exams_app/net/Address.dart';
import 'package:exams_app/net/DioManager.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

class MyExamsLogic extends GetxController{

  int page = 0;

  Rx<MyExamsModel> model;

  EasyRefreshController esController = EasyRefreshController();

  var dataList = List();


  /// 空数据

  emptyData(){
    if(dataList.length == 0||model.value.message=='no'){
      return Container(
        // width: double.infinity,
        // height: double.infinity,
        child: Center(
          child: SizedBox(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(

                    child: Column(
                      children: [
                        Image.asset(A.assets_nodata_icon,width: 100,height: 100,),
                        Text('暂无数据',style: TextStyle(fontSize: 13,fontWeight: FontWeight.w600),),
                      ],
                    )
                ),
              ],
            ),
          ),
        ),
      );
    }else{
      return null;
    }
  }

  /// 下拉刷新

  Future<void> onRefresh()async{
    page = 0;
    requestData(true,page: page);
  }
  ///  上拉加载更多
  Future<void> onLoad()async{
    page++;
    requestData(false,page: page);
    await Future.delayed(Duration(milliseconds: 250), () {

    });
  }

  /// 列表
  void requestData(bool isHeader,{int page = 0})async{

    var params = {
      'domain': "[('user_id', '=', ${await PersistentStorage().getStorage('uid')})]",
      'limit': 10,                       //   #每页显示数量
      'order': 'id desc',               //    #排序
      'page': page,
    };
    String result = await DioUtils(url: await PersistentStorage().getStorage('urlStr')).postModel(Address.getattt,model: 'res.exam',function_name: '_api_page',params:params );

    Map json = jsonDecode(result);

    model = MyExamsModel.fromJson(json).obs;


    if(page == 0){

      esController.resetLoadState();
      dataList.clear();
      dataList.addAll(model.value.data);
    }else{

      if(json['data'].length>0){
        // for(int i = 0;i<model.data.length;i++){
        //   model.data[i].isSelect = false;
        // }

        dataList.addAll(model.value.data);
      }else if(json['data'].length <= 0||json['data']==null){
        // _esController.finishLoad(success: true,noMore: true);
        esController.finishLoad(success: true, noMore: true);
        BotToast.showText(text: '暂无更多');
      }
    }

    update();

  }

  @override
  void onInit() {
    // TODO: implement onInit
    requestData(true,page: 0);

    super.onInit();

  }

}


class MyExamsModel {
  String search;
  List<Data> data;
  int searchCount;
  int errcode;
  String errmsg;
  String message;

  MyExamsModel(
      {this.search,
        this.data,
        this.searchCount,
        this.errcode,
        this.errmsg,
        this.message});

  MyExamsModel.fromJson(Map<String, dynamic> json) {
    search = json['search'];
    if (json['data'] != null) {
      data = new List<Data>();
      json['data'].forEach((v) {
        data.add(new Data.fromJson(v));
      });
    }
    searchCount = json['search_count'];
    errcode = json['errcode'];
    errmsg = json['errmsg'];
    message = json['message'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['search'] = this.search;
    if (this.data != null) {
      data['data'] = this.data.map((v) => v.toJson()).toList();
    }
    data['search_count'] = this.searchCount;
    data['errcode'] = this.errcode;
    data['errmsg'] = this.errmsg;
    data['message'] = this.message;
    return data;
  }
}

class Data {
  var image128;
  var pastTime;
  var createDate;
  var id;
  var name;
  var textName;
  var dateOrder;
  var dateStart;
  var dateEnd;
  var planIdName;
  var state;
  var scores;
  var error;
  var right;
  var type;

  Data(
      {this.image128,
        this.pastTime,
        this.createDate,
        this.id,
        this.name,
        this.textName,
        this.dateOrder,
        this.dateStart,
        this.dateEnd,
        this.planIdName,
        this.state,
        this.scores,
        this.error,
        this.right,
        this.type});

  Data.fromJson(Map<String, dynamic> json) {
    image128 = json['image_128'];
    pastTime = json['past_time'];
    createDate = json['create_date'];
    id = json['id'];
    name = json['name'];
    textName = json['text_name'];
    dateOrder = json['date_order'];
    dateStart = json['date_start'];
    dateEnd = json['date_end'];
    planIdName = json['plan_id_name'];
    state = json['state'];
    scores = json['scores'];
    error = json['error'];
    right = json['right'];
    type = json['type'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['image_128'] = this.image128;
    data['past_time'] = this.pastTime;
    data['create_date'] = this.createDate;
    data['id'] = this.id;
    data['name'] = this.name;
    data['text_name'] = this.textName;
    data['date_order'] = this.dateOrder;
    data['date_start'] = this.dateStart;
    data['date_end'] = this.dateEnd;
    data['plan_id_name'] = this.planIdName;
    data['state'] = this.state;
    data['scores'] = this.scores;
    data['error'] = this.error;
    data['right'] = this.right;
    data['type'] = this.type;
    return data;
  }
}

